﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using Ivy.UserRegistration.DataAccess;
using Ivy.UserRegistration.Entity;
using Ivy.Common.Framework.GeneralUtility;
using Ivy.Common.Framework.EmailUtility;
using Ivy.UserRegistration.IBusinessLogic;


namespace Ivy.UserRegistration.BusinessLogic
{
    public class UserRegistrationBus : IUserRegistrationBus
    {
        #region Variables & Constants
        #endregion Variables & Constants

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Public Properties
        #endregion Public Properties

        #region Public Methods
        #region IUserRegistrationBus Members
        #region UserRegistration and profile update
        public bool RegisterNewUser(User UserEntity)
        {
            bool IsOperationSuccessful = false;
            try
            {
                //First check if the user login ID is already present or not. If not preset then insert.
                if (!IsLoginIDExisting(UserEntity.UserLoginID))
                {
                    UserDao lUserDao = new UserDao();
                    IsOperationSuccessful = lUserDao.Insert(UserEntity);

                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }
            return IsOperationSuccessful;
        }

        public bool SendVerificationCodeEmail(User UserEntity)
        {
            bool IsOperationSuccessful = false;
            try
            {
                EmailUtility lEmailUtility = new EmailUtility();
                MailMessage lMailMsg = new MailMessage();
                lMailMsg.Subject = "Framework Ivy - Registration verification & confirmation mail";
                lMailMsg.IsBodyHtml = true;
                lMailMsg.Body = @"<html xmlns=""http://www.w3.org/1999/xhtml"" xml:lang=""en"">";
                lMailMsg.Body = lMailMsg.Body + @"<head> <style type=""text/css""> .style1 { color: #006600; } .style2 { text-align: left; font-size: small; } .style3 { font-size: large; } .style4 { font-size: large; font-weight: bold; } </style> </head>";
                lMailMsg.Body = lMailMsg.Body + @"<body style=""font-family: 'MS Sans Serif'; text-align: center"">";
                lMailMsg.Body = lMailMsg.Body + @"<table class=""style3"">";
                lMailMsg.Body = lMailMsg.Body + @"<tr><td><h1 class=""style1""> Congratulation ! </h1></td></tr>";
                lMailMsg.Body = lMailMsg.Body + @"<tr><td class=""style3""> You have Successfully Registered with Framework Ivy. </td></tr>";
                lMailMsg.Body = lMailMsg.Body + @"<tr><td class=""style3""> Please Note Following : </td></tr>";
                lMailMsg.Body = lMailMsg.Body + @"<tr><td><span class=""style3"">User Name : </span><span class=""style4"">" + UserEntity.UserFirstName + " " + UserEntity.UserLastName + @"</span><span class=""style3""> </span></td></tr>";
                lMailMsg.Body = lMailMsg.Body + @"<tr><td><span class=""style3"">User Login Id : </span><span class=""style4"">" + UserEntity.UserLoginID + @"</span><span class=""style3""> </span></td></tr>";
                lMailMsg.Body = lMailMsg.Body + @"<tr> <td> <span class=""style3"">User Verification Code : </span><span class=""style4"">" + UserEntity.UserVerificationCode + @"</span><span class=""style3""> </span> </td> </tr>";
                lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style3""> To activate your membership, please login and confirm your verification code </td></tr>";
                lMailMsg.Body = lMailMsg.Body + @"<tr> <td> &nbsp; </td> </tr>";
                lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style2""> Thanks and Regards </td> </tr> <tr> <td class=""style2""> Framework Ivy </td> </tr> <tr> <td class=""style2""> (An open source community) </td> </tr> <tr> <td class=""style2""> &nbsp; </td> </tr>";
                lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style2""> Follow our project at CodePlex <a href=""http://ivyframeworks.codeplex.com/""> www.ivyframeworks.codeplex.com/</a> </td> </tr>";
                lMailMsg.Body = lMailMsg.Body + @"<tr> <td class=""style2""> Copyright &copy; 2012&nbsp; <a href=""http://www.frameworkivy.com/"">www.frameworkivy.com</a> </td></tr>";
                lMailMsg.Body = lMailMsg.Body + @"</table></body></html>";

                lEmailUtility.AddEmailSenderAddressToMailMessage(lMailMsg, "frameworkivy@gmail.com", "Administrator FrameworkIvy");
                lEmailUtility.AddEmailRecepientOrReplyAddressesToMailMessage(lMailMsg, EmailAddressType.SendTo, UserEntity.UserPrimaryEmailID);
                lEmailUtility.AddEmailRecepientOrReplyAddressesToMailMessage(lMailMsg, EmailAddressType.SendBcc, "frameworkivy@googlegroups.com");

                lEmailUtility.SendEmail(lMailMsg);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }
            return IsOperationSuccessful;
        }

        public bool UpdateRegisteredUser(User UserEntity)
        {
            bool IsOperationSuccessful = false;
            try
            {

                //if (Login(UserEntity.UserLoginID, UserEntity.UserPassword) == true)
                //{
                UserDao lUserDao = new UserDao();
                IsOperationSuccessful = lUserDao.Update(UserEntity);
                //}
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }
            return IsOperationSuccessful;
        }

        public bool SaveUserProfile(UserProfile UserProfileEntity)
        {
            bool IsOperationSuccessful = false;
            try
            {
                UserDao lUserDao = new UserDao();
                if (UserProfileEntity.UserKey > 0)
                {
                    User lUser = lUserDao.GetByID(UserProfileEntity.UserKey.ToString());
                    if (lUser != null)
                    {
                        UserProfileDao _UserProfileDao = new UserProfileDao();
                        UserProfile _UserProfile = _UserProfileDao.GetByID(UserProfileEntity.UserKey.ToString());
                        if (_UserProfile != null)
                        {
                            _UserProfileDao = new UserProfileDao();
                            IsOperationSuccessful = _UserProfileDao.Update(UserProfileEntity);
                        }
                        else
                        {
                            _UserProfileDao = new UserProfileDao();
                            IsOperationSuccessful = _UserProfileDao.Insert(UserProfileEntity);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }
            return IsOperationSuccessful;
        }

        public bool IsLoginIDExisting(string UserLoginID)
        {
            bool IsLoginIDExisting = true;
            try
            {
                UserDao lUserDao = new UserDao();
                User lUser;
                lUser = lUserDao.GetByLoginID(UserLoginID);
                if (lUser == null)
                {
                    IsLoginIDExisting = false;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }
            return IsLoginIDExisting;
        }

        public bool IsUserVerified(string UserLoginID)
        {
            bool IsUserVerified = false;
            try
            {
                UserDao lUserDao = new UserDao();
                User lUser;
                lUser = lUserDao.GetByLoginID(UserLoginID);
                if (lUser.UserIsVerified == true)
                {
                    IsUserVerified = true;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }
            return IsUserVerified;
        }

        public User GetUserByLoginID(string UserLoginID)
        {
            try
            {
                UserDao lUserDao = new UserDao();
                User lUser;
                lUser = lUserDao.GetByLoginID(UserLoginID);
                if (lUser == null)
                {
                    return null;
                }
                else
                {
                    return lUser;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }
        }

        public bool Login(string UserLoginID, string Password)
        {
            bool IsLoginSuccess = false;
            try
            {
                UserDao lUserDao = new UserDao();
                User lUser;
                lUser = lUserDao.GetByLoginID(UserLoginID);
                if (lUser != null)
                {
                    if (lUser.UserLoginID == UserLoginID)
                    {
                        if (StringUtility.DecryptString(lUser.UserPassword) == Password)
                        {
                            IsLoginSuccess = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }
            return IsLoginSuccess;
        }

        public UserProfile GetUserProfileByID(string UserID)
        {
            try
            {
                UserProfileDao lUserProfileDao = new UserProfileDao();
                UserProfile lUserProfile = lUserProfileDao.GetByID(UserID);
                if (lUserProfile == null)
                {
                    return null;
                }
                else
                {
                    return lUserProfile;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }
        }
        #endregion UserRegistration and profile update


        #region User Society and Role management
        public bool AssignSocietyAndRoleToUser(int UserKey, int SocietyKey, int RoleKey)
        {
            throw new NotFiniteNumberException();
        }

        public bool RemoveSocietyAndRoleFromUser(int UserKey, int SocietyKey, int RoleKey)
        {
            throw new NotFiniteNumberException();
        }

        public List<UserRole> GetAllRolesForSelectedUser(int UserKey)
        {
            List<UserRole> lUserRoles = new List<UserRole>();
            try
            {
                UserRoleDao lUserRoleDao = new UserRoleDao();
                lUserRoles = lUserRoleDao.GetByUserKey(UserKey);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }
            return lUserRoles;
        }
 
        public Sys_RoleMaster GetSelectedSys_RoleMasterByID(int Rolekey)
        {

            try
            {
                Sys_RoleMasterDao lSys_RoleMasterDao = new Sys_RoleMasterDao();
                Sys_RoleMaster lSys_RoleMaster;
                lSys_RoleMaster = lSys_RoleMasterDao.GetByRoleKey(Rolekey);
                if (lSys_RoleMaster == null)
                {
                    return null;
                }
                else
                {
                    return lSys_RoleMaster;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                throw ex;
            }
        }
        #endregion User Society and Role management
        #endregion
        #endregion Public Methods

        #region Events
        #endregion Events

        #region Private Methods
        #endregion Private Methods


    }
}
